<?php
class FileHandler {
	
	function MakeLocationString( $sRoot, $sAdditional ) {
		return $sRoot . DIRECTORY_SEPARATOR . $sAdditional;
	}
	
	function FindDirectoryInIncludePath( $sDirectory ) {
		$sIncludePath = ini_get( 'include_path' );
		$aIncDirectories = array_map( 'trim', explode( ';', $sIncludePath ) );
	
		foreach( $aIncDirectories as $sThisIncPath ) {
			$sCompleteDirectory = FileHandler::MakeLocationString( $sThisIncPath, $sDirectory );
			if ( file_exists( $sCompleteDirectory ) ) {
				return $sCompleteDirectory;
			}
		}
		return false;
	}
	
	function IsFileMatchingRegEx( $sDirectory, $sFile, $sRegEx ) {
		return is_file( FileHandler::MakeLocationString( $sDirectory, $sFile ) ) && ( preg_match( $sRegEx, $sFile ) );
	}

	function IsDirectory( $sBaseDirectory, $sSubDirectory ) {
		if ( $sSubDirectory == false || $sSubDirectory == '.' || $sSubDirectory == '..' || $sSubDirectory == '.svn' || $sSubDirectory == 'CVS' ) {
			return false;	
		}
		$sDirectory = FileHandler::MakeLocationString( $sBaseDirectory, $sSubDirectory );
		return ( is_dir( $sDirectory ) );
	}
	
	function GetListingFromDirectory( $sDirectory ) {
		$aDirectoryListing = array();
	    $oDirectory = dir( $sDirectory );
	    while ( false !== ( $aDirectoryListing[] = $oDirectory->read() ) );
	    $oDirectory->close();
	    sort( $aDirectoryListing );
	    return $aDirectoryListing;
	}
	
	function MatchDirectoryEntries( $sDirectory, $sMethodCall, $sSearchRegEx = '/.*/' ) {
		$aFullDirectoryListing = FileHandler::GetListingFromDirectory( $sDirectory );
		$aReturnDirectoryListing = array();
		foreach( $aFullDirectoryListing as $sThisDirectoryEntry ) {
	    	if ( FileHandler::$sMethodCall( $sDirectory, $sThisDirectoryEntry, $sSearchRegEx ) ) {
				$aReturnDirectoryListing[] = FileHandler::MakeLocationString( $sDirectory, $sThisDirectoryEntry );
	    	}
	    }
	    return $aReturnDirectoryListing;
	}
	
	function GetFileListingFromDirectory( $sDirectory, $sFileSearchRegEx, $bRecursive ) {

		$aFileList = FileHandler::MatchDirectoryEntries( $sDirectory, 'IsFileMatchingRegEx', $sFileSearchRegEx );

		if ( $bRecursive ) {
			$aSubDirectories = FileHandler::GetDirectoryListingFromDirectory( $sDirectory );
			foreach ( $aSubDirectories as $sThisSubDirectory ) {
				$aFileList = array_merge( $aFileList, FileHandler::GetFileListingFromDirectory( $sThisSubDirectory, $sFileSearchRegEx, $bRecursive ) );
			}
		}
		return $aFileList;
	}

	function GetDirectoryListingFromDirectory( $sDirectory ) {
		return FileHandler::MatchDirectoryEntries( $sDirectory, 'IsDirectory' );
	}

	function GetFileListingFromDirectoryInIncludePath( $sRawDirectory, $sFileSearchRegEx, $bRecursive ) {
		$sDirectory = FileHandler::FindDirectoryInIncludePath( $sRawDirectory );
		if ( ! $sDirectory ){
			return array();
		}
		return FileHandler::GetFileListingFromDirectory( $sDirectory, $sFileSearchRegEx, $bRecursive );
	}
	
	function WriteFile( $sFilename, $sContent ) {
		$oFileHandle = fopen( $sFilename, 'w' );
		if ( !$oFileHandle ) {
			return false;
		}
		$bSuccess = fwrite( $oFileHandle, $sContent );
		fclose( $oFileHandle );
		return $bSuccess;
	}
	
	function ReadFromStream( &$oStream ) {
		$xContents = '';
		while ( !feof( $oStream ) ) {
			$xContents .= fread( $oStream, 8192 );
		}
		fclose( $oStream );
		return $xContents;
	}
}
?>
